import {observable, action} from 'mobx';
import { Tools, Toast } from 'weaver-mobile';
import * as API from '../../api/signInfo'
import { MapPage, Signature } from 'weaver-mobile-page';
const getCurrentPosition = MapPage.getCurrentPosition;
const getLabel = Tools.getLabel;
export default class SignInfoStore {

  @observable signStatus = ''
  @observable signTime = '';
  @observable signBackTime = '';
  
  @observable canview = false
  //纬度
  @observable latitude  = ''
  //经度
  @observable longitude  = ''
  @observable meetingInfo = {}
  @observable visible = 'block'
  @observable signMsg = {
    '0':getLabel(390567,'您已成功签到以下会议'),
    '-1':getLabel(129699,'无法获取签到用户'),
    '-2':getLabel(129700,'二维码无效'),
    '-3':getLabel(129701,'会议未找到或者已经取消'),
    '-4':getLabel(129702,'会议已结束'),
    '-5':getLabel(514151,'该会议还未开始签到'),
    '-6':getLabel(129706,' 已经签到过,不需要重复签到 '),
    '-7':getLabel(129707,'非参会人员签到初始化失败'),
    '-8':getLabel(129708,'非法签到'),
    '-9':getLabel(390462,'二维码已失效.请刷新二维码'),
    '-1024':getLabel(129709,'签到失败'),
    '-10': getLabel('510285','您已成功签退以下会议'),
    '-11': getLabel('510284','您已签退以下会议') , // 早退
    '-12': getLabel('510286','该会议已不能签退') ,
    '-13': getLabel('521771','签到失败，非参会人员不允许签到'),
    '-14': getLabel('528006','签退失败，未启用签退功能。'),
    '-15': getLabel('522476','签退失败，签到{minute}分钟后才能签退'),

  };
  @observable meetingid = '';
  @observable subLoadding = false;
  
  @observable signatureStore = new Signature.Store({ route: '/calendar/detail/sign', id: 'signature' });

  @action
  setState = (params={}) => {
    Object.keys(params).forEach(key => {
      this[key] = params[key];
    });
  }
  signAndGetAddress = (params = {},callback) => {
    if(window.em) {
      window.em.ready(() => {
        this.getPositionAndSign(params,callback);
      });
    } else {
        this.signMeeting(params,callback);
    }
    
  }

  getPositionAndSign = (params,callback)=>{
    Toast.loading(getLabel('512753','获取地理位置'), 1, () => {
      getCurrentPosition().then((res)=>{
        //获取地址,如果获取不到返回status==0,没有position属性返回,所以用position更能体现是否已经启用
        if(res.position){
          Toast.success(getLabel('512751','获取成功') , 1);
          params = {...params,latitude:res.position[0],longitude:res.position[1],address:res.address}
        }else{
          Toast.fail(getLabel('127889','获取失败') , 1);
          params = {...params,latitude:'',longitude:'',address:''}
        }
        this.signMeeting(params,callback)
      })
    });
  }

  signMeeting = (params = {},callback) => {
    API.signMeeting(params).then((result)=>{
      if(result && result.api_status){
        this.signStatus = result.ret + ''
        this.meetingInfo = result.meetingInfo
        this.signTime = result.signTime;
        this.signBackTime = result.signBackTime;
        this.canview = result.canview
        this.visible = 'none'
        if(result.ret=="0"||result.ret=='-6'){
          window.em && window.em.checkJsApi('changeTitle') && window.em.changeTitle({ title: getLabel(129698, '签到成功') })
        }else if(result.ret=='-7'||result.ret=='-5'||result.ret=='-1024'||result.ret=='-8'||result.ret=='-1' ||result.ret=='-13'){
          window.em && window.em.checkJsApi('changeTitle') && window.em.changeTitle({ title: getLabel(129709, '签到失败') })
        }else if(result.ret=='-11'||result.ret=='-10'){
          window.em && window.em.checkJsApi('changeTitle') && window.em.changeTitle({ title: getLabel(514367, '签退成功') })
        }else if(result.ret=='-12'||result.ret=='-14'){
          window.em && window.em.checkJsApi('changeTitle') && window.em.changeTitle({ title: getLabel(514368, '签退失败') })
        }else if(result.ret.indexOf('-15')>-1){
          window.em && window.em.checkJsApi('changeTitle') && window.em.changeTitle({ title: getLabel(514368, '签退失败') })
        }
        if(typeof callback === 'function'){
          callback();
        }
      }
    })
  }

  @action
  saveRemarkAndGetAddress = (params) => {
    this.subLoadding = true;
    getCurrentPosition().then((res)=>{
      params = {...params,latitude:res.position[0],longitude:res.position[1],address:res.address,id:this.signStatus}
      API.saveRemark(params).then((result)=>{
        this.subLoadding = false;
        if(result && result.api_status){
          this.signStatus = result.ret + ''
          this.meetingInfo = result.meetingInfo
          this.signTime = result.signTime
          this.canview = result.canview
          this.visible = 'none'
        }
      })
    })
  }

  @action
  saveRemark = (params) => {
    this.subLoadding = true;
    params = {...params,id:this.signStatus}
    API.saveRemark(params).then((result)=>{
      this.subLoadding = false;
      if(result && result.api_status){
        this.signStatus = result.ret + ''
        this.meetingInfo = result.meetingInfo
        this.signTime = result.signTime
        this.canview = result.canview
        this.visible = 'none'
      }
    })
  }
}